//
//  HSForumListVC.m
//  HolyShit
//
//  Created by Robin on 2017/4/4.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSForumListVC.h"
#import "HSForumListVM.h"
#import "HSForumListCell.h"
#import "HSTopicListVC.h"

@interface HSForumListVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HSForumListVM *listVM;

@end

@implementation HSForumListVC

- (instancetype)init
{
    if (self = [super init]) {
        self.title = @"社区";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = nil;
    [self.view addSubview:self.tableView];
    @weakify(self);
    [self.hintView setReloadBlock:^{
        @strongify(self);
        self.hintView.hintState = MYHintStateLoading;
        [self refresh];
    }];
    self.hintView.reloadBlock();
}

- (void)refresh
{
    @weakify(self);
    [[self.listVM.dataCommand execute:nil] subscribeNext:^(id x) {
        @strongify(self);
        self.hintView.hintState = MYHintStateHidden;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
        [UIWindow showError:error];
    }];
}

#pragma mark - uitableview

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listVM.forums.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ForumListStyle == 1 ? 60 : 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSForumListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HSForumListCell class])];
    cell.model = self.listVM.forums[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSForumListModel *model = self.listVM.forums[indexPath.row];
    [self my_push:[[HSTopicListVC alloc] initWithName:model.attributes.name slug:model.attributes.slug]];
}

#pragma mark - getter

- (HSForumListVM *)listVM{
    if(!_listVM){
        _listVM = [HSForumListVM new];
    }
    return _listVM;
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - NavBarHeight - TabbarHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = CLEAR_COLOR;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView addRefreshTarget:self action:@selector(refresh)];
        [_tableView registerClass:[HSForumListCell class] forCellReuseIdentifier:NSStringFromClass([HSForumListCell class])];
    }
    return _tableView;
}

@end
